package com.platform.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.platform.entity.ApiMessageEntity;
import com.platform.entity.FeedbackEntity;
import com.platform.entity.UserEntity;
import com.platform.service.ApiMessageService;
import com.platform.service.FeedbackService;
import com.platform.service.UserService;
import com.platform.utils.push.UPush;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.service.ApiFeedbackService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-07-11 17:09:50
 */
@Controller
@RequestMapping("apifeedback")
public class FeedbackController extends AbstractController {
    @Autowired
    private FeedbackService apiFeedbackService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiMessageService apiMessageService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("feedback:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<FeedbackEntity> apiFeedbackList = apiFeedbackService.queryList(query);
        for (FeedbackEntity info : apiFeedbackList) {
            UserEntity userEntity = userService.queryObject(info.getUserId());
            if (userEntity != null) {
                info.setUserName(userEntity.getNickname());
            }
        }


        int total = apiFeedbackService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiFeedbackList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("feedback:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        FeedbackEntity apiFeedback = apiFeedbackService.queryObject(id);

        UserEntity userEntity = userService.queryObject(apiFeedback.getUserId());
        if (userEntity != null) {
            apiFeedback.setUserName(userEntity.getNickname());
        }

        return R.ok().put("apiFeedback", apiFeedback);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("feedback:save")
    @ResponseBody
    public R save(@RequestBody FeedbackEntity apiFeedback) {
        apiFeedbackService.save(apiFeedback);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("feedback:update")
    @ResponseBody
    public R update(@RequestBody FeedbackEntity apiFeedback) {
        apiFeedback.setStatus(2);
        apiFeedback.setSysUserId(getUserId());
        apiFeedback.setSysTime(new Date());
        apiFeedbackService.update(apiFeedback);

        UserEntity userEntity = userService.queryObject(apiFeedback.getUserId());

        ApiMessageEntity messageEntity = new ApiMessageEntity();
        messageEntity.setCreateTime(new Date());
        messageEntity.setTitle("反馈信息");
        messageEntity.setContent("问题：" + apiFeedback.getContent() + "回复为：" + apiFeedback.getRemark());
        messageEntity.setUserId(apiFeedback.getUserId());
        apiMessageService.save(messageEntity);

        UPush.push(userEntity.getMobileType(), userEntity.getDeviceToken(), "反馈信息", apiFeedback.getRemark());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("feedback:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        apiFeedbackService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<FeedbackEntity> list = apiFeedbackService.queryList(params);

        return R.ok().put("list", list);
    }
}
